﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LabMS.Web.Equipment.Valuables
{
    public partial class valuablesrepair : Base.BasePage
    {
        public string strID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GZSBWX");//必须写在ispostback外
            strPageUserType.Add("mana");

            if (Request.QueryString["ID"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            strID = Request.QueryString["ID"].ToString();
            if (!IsPostBack)
            {
                ExtendModule.GoodsMaintain mgoods = checkQueryString(strID);
                if (mgoods == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "数据不存在!");
                    return;
                }

                tbLabNum.Attributes.Add("readOnly", "-1");
                tbManager.Attributes.Add("readOnly", "-1");
                tbReportTime.Attributes.Add("readOnly", "-1");
                tbTelNum.Attributes.Add("readOnly", "-1");
                tbStoreAdd.Attributes.Add("readOnly", "-1");
                tbLowname.Attributes.Add("readOnly", "-1");
                tbLowNum.Attributes.Add("readOnly", "-1");
                tbApplyer.Attributes.Add("readOnly", "-1");
                isInTime.Attributes.Add("readOnly", "-1");
                tbDescription.Attributes.Add("readOnly", "-1");

                Bind(mgoods);
            }
        }

        ExtendModule.GoodsMaintain checkQueryString(string strID)
        {
            ExtendBLL.GoodsMaintain bgoods = new ExtendBLL.GoodsMaintain();
            string strWhere = " Type = 'guizhongyiqi' and GoodsMaintain.ID =" + strID;
            List<ExtendModule.GoodsMaintain> mgoods = bgoods.GetExtendModuleList(strWhere);
            if (mgoods.Count <= 0)
            {
                return null;
            }
            return mgoods[0];
        }

        public void Bind(ExtendModule.GoodsMaintain mgoods)
        {
            tbLabNum.Text = mgoods.LabName.ToString();
            tbManager.Text = mgoods.AssetManagers.ToString();

            try
            {
                tbReportTime.Text = mgoods.ReportedTime.Value.ToShortDateString();
            }
            catch { }

            tbTelNum.Text = mgoods.Tel.ToString();
            tbStoreAdd.Text = mgoods.StorageSites.ToString();
            tbLowname.Text = mgoods.Name.ToString();
            tbLowNum.Text = mgoods.Numbers.ToString();
            tbApplyer.Text = mgoods.Applicant.ToString();
            isInTime.SelectedValue = mgoods.Guarantee.ToString();
            tbDescription.Text = mgoods.Describe.ToString();
        }

        protected void Submit(string status)
        {
            Model.GoodsMaintain mgoods = new LabMS.Model.GoodsMaintain();
            BLL.GoodsMaintain bgoods = new LabMS.BLL.GoodsMaintain();

            mgoods = bgoods.GetModel(long.Parse(strID));
            mgoods.LabName = tbLabNum.Text.Trim();
            mgoods.AssetManagers = tbManager.Text.Trim();
            mgoods.Tel = tbTelNum.Text.Trim();
            mgoods.Numbers = tbLowNum.Text.Trim();
            mgoods.Name = tbLowname.Text.Trim();
            mgoods.StorageSites = tbStoreAdd.Text.Trim();
            mgoods.Applicant = tbApplyer.Text.Trim();
            mgoods.Guarantee = isInTime.SelectedValue;
            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(tbReportTime.Text.Trim(), out tempTime))
            {
                mgoods.ReportedTime = tempTime;
            }
            mgoods.Describe = tbDescription.Text.Trim();
            mgoods.Status = status;
            mgoods.MCo = tbRepairGroup.Text.Trim();
            mgoods.MLinkman = tbTransfer.Text.Trim();
            mgoods.MPhone = tbPhoneNum.Text.Trim();
            if (DateTime.TryParse(tbRepairTime.Text.Trim(), out tempTime))
            {
                mgoods.MDate = tempTime;
            }
            mgoods.MComment = tbComments.Text.Trim();

            try
            {
                bgoods.Update(mgoods);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "ModifySuccess", "操作成功!", "valuablesrepairlist.aspx");
            }
            catch (Exception ex)
            {
                Err.Text = ex.Message.ToString();
            }
        }

        protected void lbDiscard_Click(object sender, EventArgs e)
        {
            Submit("报废");
        }

        protected void lbRepaired_Click(object sender, EventArgs e)
        {
            Submit("修复");
        }

        protected void lbBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("valuablesrepairlist.aspx");
        }
    }
}
